package org.example.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.example.model.User;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.*;

@Tag(name = "用户管理", description = "用户相关接口")
@RestController
@RequestMapping("/users")
@Validated
public class UserController {
    private final Map<Long, User> userMap = new HashMap<>();

    @Operation(summary = "获取所有用户")
    @ApiResponse(responseCode = "200", description = "返回用户列表")
    @GetMapping
    public List<User> list() {
        return new ArrayList<>(userMap.values());
    }

    @Operation(summary = "根据ID获取用户")
    @ApiResponses({
        @ApiResponse(responseCode = "200", description = "返回用户信息"),
        @ApiResponse(responseCode = "404", description = "用户不存在")
    })
    @GetMapping("/{id}")
    public User get(
            @Parameter(description = "用户ID", example = "1")
            @PathVariable Long id) {
        return userMap.get(id);
    }

    @Operation(summary = "新增用户")
    @ApiResponse(responseCode = "201", description = "创建成功")
    @PostMapping
    public User add(
            @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "用户信息", required = true)
            @Valid @RequestBody User user) {
        userMap.put(user.getId(), user);
        return user;
    }

    @Operation(summary = "删除用户")
    @ApiResponse(responseCode = "204", description = "删除成功，无返回内容")
    @DeleteMapping("/{id}")
    public void delete(
            @Parameter(description = "用户ID", example = "1")
            @PathVariable Long id) {
        userMap.remove(id);
    }
} 